package com.java.controller;

import com.github.pagehelper.PageInfo;
import com.java.model.*;
import com.java.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/media")
public class MediaController {
    @Autowired
    private MediaService mediaService;

    @RequestMapping("/register")
    public String register(Model model){
        List<Media> register = mediaService.register();
        model.addAttribute("platform",register);
        return "/user/register";
    }

    @RequestMapping("/register2")
    @ResponseBody
    public Result register2(Media media){
        media.setStatus(2);
        return mediaService.register2(media);
    }
    //查询入驻状态
    @RequestMapping("/openState")
    public String openState(){
        return "state";
    }
    //查询入驻状态
    @RequestMapping("/State")
    @ResponseBody
    public Result State(Media media){
        return mediaService.state(media);
    }
    /*管理员同意入驻申请*/
    @RequestMapping("/yes")
    @ResponseBody
    public Result yes2(Media media) {
        media.setStatus(1);
        return mediaService.yes(media);
    }
    /*管理员拒绝入驻申请*/
    @RequestMapping("/no")
    @ResponseBody
    public Result no(Media media) {
        media.setStatus(3);
        return mediaService.yes(media);
    }
    @RequestMapping("/enter")
    public String enter(){
        return "/admin/media/enter";
    }

    @RequestMapping("/enterList")
    @ResponseBody
    public Result enterList(Media media){
        PageInfo<Media> mediaPageInfo = mediaService.enterList(media);
        return Result.success(mediaPageInfo.getList(),mediaPageInfo.getTotal());
    }

    @RequestMapping("/mediaList")
    public String advertiserList(){
        return "/admin/media/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Result list(Media media){
        PageInfo<Media> pageInfo = mediaService.list2(media);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }

    @RequestMapping("/add")
    public String add() {
        return "/admin/media/add";
    }



    @RequestMapping("/listWeibo")
    @ResponseBody
    public Result listWeibo(Media media) {
        PageInfo<Media> pageInfo = mediaService.listWeibo(media);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }
    @RequestMapping("/toListWeibo")
    public String toListWeibo() {
        return "/admin/resource/weibo/list";
    }

    @RequestMapping("/listRedBook")
    @ResponseBody
    public Result listRedBook(Media media) {
        PageInfo<Media> pageInfo = mediaService.listRedBook(media);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }
    @RequestMapping("/toListRedBook")
    public String toListRedBook() {
        return "/admin/resource/redbook/list";
    }

    @RequestMapping("/listDouYin")
    @ResponseBody
    public Result listDouYin(Media media) {
        PageInfo<Media> pageInfo = mediaService.listDouYin(media);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }
    @RequestMapping("/toListDouYin")
    public String toListDouYin() {
        return "/admin/resource/douyin/list";
    }

    @RequestMapping("/listBiliBili")
    @ResponseBody
    public Result listBiliBili(Media media) {
        PageInfo<Media> pageInfo = mediaService.listBiliBili(media);
        return Result.success(pageInfo.getList(),pageInfo.getTotal());
    }
    @RequestMapping("/toListBiliBili")
    public String toListBiliBili() {
        return "/admin/resource/bilibili/list";
    }

    //编辑个人信息
    @RequestMapping("/edit")
    public String edit(int id, Model model) {
        Media media = mediaService.get(id);
            model.addAttribute("media", media);

        return "/admin/resource/edit";
    }
    @RequestMapping("/modify")
    @ResponseBody
    public Result modify(Media media, Model model) {
        model.addAttribute("media",media);
        return mediaService.modify(media);
    }


    //自媒体账号编辑个人信息
    @RequestMapping("/edit2")
    public String edit2(int id, Model model) {
        Media media = mediaService.get2(id);
        model.addAttribute("media", media);

        return "/admin/media/edit";
    }
    @RequestMapping("/modify2")
    @ResponseBody
    public Result modify2(@RequestBody Media media, Model model) {
        model.addAttribute("media",media);
        return mediaService.modify2(media);
    }

    @RequestMapping("/avatar")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request, Model model) {
        // 获取上传文件的名称
        String filename = multipartFile.getOriginalFilename();
        // 生成UUID
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 获取上传目录
        String realPath = request.getSession().getServletContext().getRealPath(request.getContextPath()) + "/image";
        // 上传对象
        File uploadFile = new File(realPath, uuid + filename);
        try {
            // 上传文件
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String str = "";
        str = "/image/"+uuid+filename;
        model.addAttribute("headImg",str);
        return Result.success(str,  null) ;
    }


    @RequestMapping("/list3")
    @ResponseBody
    public Result list3(Media media) {
        return mediaService.list3(media);
    }
}
